//
//  DetialViewController.m
//  又是豆瓣
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DetialViewController.h"
#import "UIImageView+WebCache.h"
#import "actModel.h"
#import "DBmethod.h"

@interface DetialViewController ()<UIScrollViewAccessibilityDelegate>

@property (nonatomic, retain) UIScrollView *scroll;

@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIImageView *imgV;
@property (nonatomic, retain) UIImageView *imageV1;
@property (nonatomic, retain) UIImageView *imageV2;
@property (nonatomic, retain) UIImageView *imageV3;
@property (nonatomic, retain) UIImageView *imageV4;
@property (nonatomic, retain) UILabel *timeLabel;
@property (nonatomic, retain) UILabel *addressLabel;
@property (nonatomic, retain) UILabel *typeLabel;
@property (nonatomic, retain) UILabel *owner;
@property (nonatomic, retain) UILabel *content;
@property (nonatomic, retain) UILabel *detial;

@property (nonatomic, retain) UIBarButtonItem *rightButtona;
@property (nonatomic, retain) UIBarButtonItem *rightButtonb;
@property (nonatomic, assign) NSInteger temp;


@end

@implementation DetialViewController

#define WI self.view.frame.size.width
#define HE self.view.frame.size.height

- (void)dealloc {
    [super dealloc];
    [_scroll release];
    [_titleLabel release];
    [_imgV release];
    [_imageV1 release];
    [_imageV2 release];
    [_imageV3 release];
    [_imageV4 release];
    [_timeLabel release];
    [_addressLabel release];
    [_typeLabel release];
    [_owner release];
    [_content release];
    [_detial release];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WI, HE)];
    _scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scroll];
    [_scroll release];
    
    _scroll.delegate = self;
    
    //自适应高度
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    CGRect rect = [_model.content boundingRectWithSize:CGSizeMake(335, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    
    
    _scroll.contentSize = CGSizeMake(0, 300 + rect.size.height);
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 50)];
    _titleLabel.text = self.model.title;
    [_scroll addSubview:_titleLabel];
    [_titleLabel release];
    
    self.imgV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 70, 100, 150)];
    [_imgV sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    [_scroll addSubview:_imgV];
    [_imgV release];
    
    self.imageV1 = [[UIImageView alloc]initWithFrame:CGRectMake(130, 70, 18, 18)];
    self.imageV4 = [[UIImageView alloc]initWithFrame:CGRectMake(130, 100, 18, 18)];
    self.imageV2 = [[UIImageView alloc]initWithFrame:CGRectMake(130, 130, 18, 18)];
    self.imageV3 = [[UIImageView alloc]initWithFrame:CGRectMake(130, 160, 18, 18)];
    
    _imageV1.image = [UIImage imageNamed:@"icon_date_blue.png"];
    _imageV2.image = [UIImage imageNamed:@"icon_catalog_blue.png"];
    _imageV3.image = [UIImage imageNamed:@"icon_spot_blue.png"];
    _imageV4.image = [UIImage imageNamed:@"icon_sponsor_blue@2x.png"];
    
    [_scroll addSubview:_imageV4];
    [_scroll addSubview:_imageV1];
    [_scroll addSubview: _imageV2];
    [_scroll addSubview:_imageV3];
    
    [_imageV1 release];
    [_imageV2 release];
    [_imageV3 release];
    [_imageV4 release];
    
    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 70, 200, 15)];
    NSString *strtime = [NSString stringWithFormat:@"%@ -- %@", [_model.begin_time substringWithRange:NSMakeRange(5, 11)], [_model.end_time substringWithRange:NSMakeRange(5, 11)]];
    _timeLabel.font = [UIFont systemFontOfSize:14];
    _timeLabel.text = strtime;
    [_scroll addSubview:_timeLabel];
    
    self.owner = [[UILabel alloc]initWithFrame:CGRectMake(150, 100, 200, 15)];
    _owner.text = _model.owner[@"name"];
    [_scroll addSubview:_owner];
    _owner.font = [UIFont systemFontOfSize:14];
    [_owner release];
    
    self.typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 130, 200, 15)];
    NSString *strType = [NSString stringWithFormat:@"类型：%@", _model.category_name];
    _typeLabel.text = strType;
    [_scroll addSubview:_typeLabel];
    _typeLabel.font = [UIFont systemFontOfSize:14];

    [_typeLabel release];
    
    self.addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 160, 200, 35)];
    _addressLabel.text = _model.address;
    [_scroll addSubview:_addressLabel];
    _addressLabel.font = [UIFont systemFontOfSize:14];
    _addressLabel.numberOfLines = 2;
    [_addressLabel release];
    
    self.detial = [[UILabel alloc]initWithFrame:CGRectMake(20, 235, 200, 50)];
    _detial.text = @"活动介绍";
    [_scroll addSubview:_detial];
    [_detial release];
    
    
    

    
    self.content = [[UILabel alloc]initWithFrame:CGRectMake(20, 285, 335, rect.size.height)];
    _content.text = _model.content;
    [_scroll addSubview:_content];
    _content.font = [UIFont systemFontOfSize:15];
    [_content release];
    _content.numberOfLines = 100;
  //  _content.backgroundColor = [UIColor redColor];
    
    
    UIImage *imga = [UIImage imageNamed:@"star_unfav@2x.png"];
    imga = [imga imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.rightButtona = [[UIBarButtonItem alloc]initWithImage:imga style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    
    
//    if ([DBmethod sharedDataBaseMethod].temp == 0) {
//
//    self.navigationItem.rightBarButtonItem = _rightButtona;
//    
//    }else  self.navigationItem.rightBarButtonItem = _rightButtonb;
    
     self.navigationItem.rightBarButtonItem = _rightButtona;
    
    UIImage *imgb = [UIImage imageNamed:@"star_faved@2x.png"];
    imgb = [imgb imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.rightButtonb = [[UIBarButtonItem alloc]initWithImage:imgb style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    
    
    
}

- (void)rightButtonAction:(UIBarButtonItem *)rightButtonItem{

    [[DBmethod sharedDataBaseMethod] createTableWithname:@"MyActivities"];
    [[DBmethod sharedDataBaseMethod] insertDataintotable:@"MyActivities" :_model ];
    
    if ([DBmethod sharedDataBaseMethod].temp == 0) {
        self.navigationItem.rightBarButtonItem = _rightButtonb;
 
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功~💃🏻" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];
    
 
    
    }
  else if([DBmethod sharedDataBaseMethod].temp == 1) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您已收藏过该商品喽~💅🏻" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"好啵~" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action2];
        [self presentViewController:alert animated:YES completion:nil];
        
        
    }

}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
